﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IQualityServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.QualityServices
{
    public class DetectionTemplateServices : IDetectionTemplateServices
    {
        public DetectionTemplateServices(IBaseRepository baseConn)
        {
            BaseConn = baseConn;
        }

        public IBaseRepository BaseConn { get; }

        public async Task<bool> AddAsync(qc_template model)
        {
            var result=await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> AddIAsync(qc_template_index model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> AddPAsync(qc_template_product model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> DelAsync(string Id)
        {
            var result=await BaseConn.DeleteAsync("delete dbo.qc_template where template_id=@Id", new {Id });
            return result;
        }

        public async Task<bool> DelAsync(string[] Id)
        {
            var result = await BaseConn.DeleteAsync("delete dbo.qc_template where template_id in @Id", new { Id });
            return result;
        }

        public async Task<bool> DelIAsync(string[] Id)
        {
            var result = await BaseConn.DeleteAsync("delete dbo.qc_template_index where record_id in @Id", new { Id });
            return result;
        }

        public async Task<bool> DelIAsync(string Id)
        {
            var result = await BaseConn.DeleteAsync("delete dbo.qc_template_index where record_id = @Id", new { Id });
            return result;
        }

        public async Task<bool> DelPAsync(string[] Id)
        {
            var result = await BaseConn.DeleteAsync("delete dbo.qc_template_product where record_id in @Id", new { Id });
            return result;
        }

        public async Task<bool> DelPAsync(string Id)
        {
            var result = await BaseConn.DeleteAsync("delete dbo.qc_template_product where record_id = @Id", new { Id });
            return result;
        }

        public async Task<int> GetCountAsync(string TCode, string TName, string PCode, string PName, string Types)
        {
            var sql = "select count(1) from dbo.qc_template where template_code like @TCode and template_name like @TName and qc_types like @Types ";
            if (!string.IsNullOrEmpty(PCode)||!string.IsNullOrEmpty(PName))
            {
                 sql = "select count(1) from dbo.qc_template t inner join dbo.qc_template_product p on t.template_id=p.template_id where t.template_code like @TCode and t.template_name like @TName and p.template_code like @PCode and p.template_name like @PName and t.qc_types like @Types";
                //if (!string.IsNullOrEmpty(Types))
                //{
                //    string[] types = Types.Split(",");
                //    for (int i = 0; i < types.Length; i++)
                //    {
                //        sql += $@" and t.qc_types like '%{types[i]}%'";
                //    }
                    
                //}
            }
            var result = await BaseConn.GetRecoreCountAsync(sql, new { TCode = "%" + TCode + "%", TName = "%" + TName + "%", PCode = "%" + PCode + "%", PName = "%" + PName + "%", Types = "%" + Types + "%" });

            return result;
        }

        public async Task<int> GetICountAsync()
        {
            var sql = "select count(1) from dbo.qc_template_index";
            var resule = await BaseConn.GetRecoreCountAsync(sql);
            return resule;
        }

        public async Task<IEnumerable<qc_template_index>> GetIJsonAsync(int PageIndex, int PageSize)
        {
            var sql = "select * from dbo.qc_template_index  order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            var resule = await BaseConn.QueryAsync<qc_template_index>(sql, new {PageIndex=PageIndex,PageSize=PageSize });
            return resule;
        }

        public async Task<IEnumerable<qc_template>> GetJsonAsync(int PageIndex, int PageSize, string TCode, string TName, string PCode, string PName, string Types)
        {
            var sql = "select * from dbo.qc_template where template_code like @TCode and template_name like @TName and qc_types like @Types order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            if (!string.IsNullOrEmpty(PCode) || !string.IsNullOrEmpty(PName))
            {
                sql = "select * from dbo.qc_template t inner join dbo.qc_template_product p on t.template_id=p.template_id where t.template_code like @TCode and t.template_name like @TName and p.template_code like @PCode and p.template_name like @PName and t.qc_types like @Types order by t.create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            }
            var result = await BaseConn.QueryAsync<qc_template>(sql, new {PageIndex=PageIndex,PageSize=PageSize, TCode = "%" + TCode + "%", TName = "%" + TName + "%", PCode = "%" + PCode + "%", PName = "%" + PName + "%", Types = "%" + Types + "%" });

            return result;
        }

        public async Task<int> GetPCountAsync()
        {
            var sql = "select count(1) from dbo.qc_template_product";
            var result = await BaseConn.GetRecoreCountAsync(sql);
            return result;
        }

        public async Task<IEnumerable<qc_template_product>> GetPJsonAsync(int PageIndex, int PageSize)
        {
            var sql = "select * from dbo.qc_template_product  order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            var resule = await BaseConn.QueryAsync<qc_template_product>(sql, new { PageIndex = PageIndex, PageSize = PageSize });
            return resule;
        }

        public async Task<bool> UpdateAsync(qc_template model)
        {
            var result=await BaseConn.UpdateAsync(model, "template_id");
            return result;
        }

        public async Task<bool> UpdateIAsync(qc_template_index model)
        {
            var result = await BaseConn.UpdateAsync(model, "record_id");
            return result;
        }

        public async Task<bool> UpdatePAsync(qc_template_product model)
        {
            var result = await BaseConn.UpdateAsync(model, "record_id");
            return result;
        }
    }
}
